package com.dalei.vdsmanager.management.vds.service;

import java.io.Serializable;
import java.util.List;

import com.dalei.vdsmanager.management.vds.dao.VDSSettings;
import com.dalei.vdsmanager.management.vds.dao.VDSSettingsDao;

public class VDSSettingsServiceImpl implements VDSSettingsService, Serializable {

	public static final String DEFAULT_GROUP = "default";

	public static final String CUSTOM_GROUP = "custom";

	/**
	 * 
	 */
	private static final long serialVersionUID = 8785694492792967551L;

	private VDSSettingsDao vdsSettingsDao;

	public VDSSettingsDao getVdsSettingsDao() {
		return vdsSettingsDao;
	}

	public void setVdsSettingsDao(VDSSettingsDao vdsSettingsDao) {
		this.vdsSettingsDao = vdsSettingsDao;
	}

	@Override
	public List<VDSSettings> getVDSSettings() {
		// TODO Auto-generated method stub
		return vdsSettingsDao.findVDSSettings();
	}

	@Override
	public VDSSettings getDefaultSettings() {
		// TODO Auto-generated method stub
		return vdsSettingsDao.findVDSSettingsByGroupName(DEFAULT_GROUP);
	}

	@Override
	public VDSSettings getCustomSettings() {
		// TODO Auto-generated method stub
		return vdsSettingsDao.findVDSSettingsByGroupName(CUSTOM_GROUP);
	}
	
	@Override
	public void updateCustomSettings(VDSSettings customSettings) {
		// TODO Auto-generated method stub
		if (customSettings != null) {
			VDSSettings settings = vdsSettingsDao
					.findVDSSettingsByGroupName(CUSTOM_GROUP);
			// If custom settings changed 
			if ((settings.getSessionTimeout() != customSettings
					.getSessionTimeout())
					|| (settings.getEnabled() != customSettings.getEnabled())) {
				// If custom settings was enabled
				if (customSettings.getEnabled()) {
					EnableCustomSettings();
				}
				settings.setSessionTimeout(customSettings.getSessionTimeout());
				settings.setEnabled(customSettings.getEnabled());
				vdsSettingsDao.updateVDSSettings(settings);
			}
		}
	}
	
	@Override
	public void EnableDefaultSettings() {
		// TODO Auto-generated method stub
		VDSSettings defaultSettings = vdsSettingsDao
				.findVDSSettingsByGroupName(DEFAULT_GROUP);
		VDSSettings customSettings = vdsSettingsDao
				.findVDSSettingsByGroupName(CUSTOM_GROUP);
		if ((defaultSettings != null) && (customSettings != null)) {
			defaultSettings.setEnabled(true);
			customSettings.setEnabled(false);
			vdsSettingsDao.updateVDSSettings(defaultSettings);
			vdsSettingsDao.updateVDSSettings(customSettings);
		}
	}

	@Override
	public void EnableCustomSettings() {
		// TODO Auto-generated method stub
		VDSSettings defaultSettings = vdsSettingsDao
				.findVDSSettingsByGroupName(DEFAULT_GROUP);
		VDSSettings customSettings = vdsSettingsDao
				.findVDSSettingsByGroupName(CUSTOM_GROUP);
		if ((defaultSettings != null) && (customSettings != null)) {
			// If the value of session timeout was not set, default settings will be enabled
			if (customSettings.getSessionTimeout() != null) {
				defaultSettings.setEnabled(false);
				customSettings.setEnabled(true);
				vdsSettingsDao.updateVDSSettings(defaultSettings);
				vdsSettingsDao.updateVDSSettings(customSettings);
			} else {
				defaultSettings.setEnabled(true);
				customSettings.setEnabled(false);
				vdsSettingsDao.updateVDSSettings(defaultSettings);
				vdsSettingsDao.updateVDSSettings(customSettings);
			}
		}
	}
}
